﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using annuairedotnet;
using Annuaire;


public partial class RechContact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String urlReferent = (String)Session["urlReferent"];
        Session.Remove("urlReferent");
        ContactCritere critereSession = (ContactCritere)Session["criteres"];

        if ((urlReferent == ("AffContactPro.aspx") || urlReferent == ("AffContactPerso.aspx") )&& (critereSession != null))
        {  
            tbNom.Text = critereSession.Nom;
            tbPrenom.Text = critereSession.Prenom;
            tbVillePerso.Text = critereSession.VillePerso;
            tbVillePro.Text = critereSession.VillePro;
            tbSociete.Text = critereSession.Societe;
            afficherDonnees();
            panel.Enabled = false;
            btAffiner.Visible = true;
            btReinitiliser.Visible = true;
            btRechercher.Visible = false;
        }
        if (Roles.IsUserInRole("admin"))
        {
            btCreer.Visible = true;
        }
    }
 
    protected void btCreer_Click(object sender, EventArgs e)
    {
      Response.Redirect("AffContactPerso.aspx");
    }

    public void afficherDonnees()
    {
        ContactCritere criteres = new ContactCritere();
        criteres.Nom = tbNom.Text;
        criteres.Prenom = tbPrenom.Text;
        criteres.Societe = tbSociete.Text;
        criteres.VillePerso = tbVillePerso.Text;
        criteres.VillePro = tbVillePro.Text;

        IList<Contact> contacts = null;

        if (tbNom.Text.Equals("") && tbPrenom.Text.Equals("") && tbSociete.Text.Equals("") && tbVillePerso.Text.Equals("") && tbVillePro.Text.Equals(""))
        {
            contacts = ContactService.searchContact(null);
        }
        else
        {
            contacts = ContactService.searchContact(criteres);
        }

        // On récupère le nombre de contacts trouvés et on l'affiche au-dessus du GridView
        if (contacts.Count >= 1)
        {
            lbNbResult.Text = contacts.Count + " contact(s)";
            lbNbResult.Visible = true;
        }

        gvResult.DataSource = contacts;
        gvResult.DataBind();
        if (!Roles.IsUserInRole("admin"))
        {
            gvResult.Columns[6].Visible = false;
        }
        gvResult.Visible = true;
    }

    protected void btRechercher_Click(object sender, EventArgs e)
    {
        panel.Enabled = false;
        btAffiner.Visible = true;
        btReinitiliser.Visible = true;
        btRechercher.Visible = false;
        gvResult.PageIndex = 0;
        AddContactsSession();
        afficherDonnees();
    }

    protected void btAffiner_Click(object sender, EventArgs e)
    {   
        panel.Enabled = true;
        btAffiner.Visible = false;
        btReinitiliser.Visible = false;
        btRechercher.Visible = true;
    }

    protected void btReinitiliser_Click1(object sender, EventArgs e)
    {
        Session.Remove("urlReferent");
        Server.Transfer("RechContact.aspx");
        panel.Enabled = true;
        btAffiner.Visible = false;
        btReinitiliser.Visible = false; 
    }
    
    protected void AddContactsSession()
    {
        ContactCritere criteres = new ContactCritere();
        criteres.Nom = tbNom.Text;
        criteres.Prenom = tbPrenom.Text;
        criteres.Societe = tbSociete.Text;
        criteres.VillePerso = tbVillePerso.Text;
        criteres.VillePro = tbVillePro.Text;

        Session.Add("criteres", criteres);
    }
    
    protected void gvResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvResult.PageIndex = e.NewPageIndex;
        afficherDonnees();
    }

    protected void gvResult_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {        
        int contactId = (int)gvResult.DataKeys[e.RowIndex].Value ;
        ContactService.removeContact(contactId);
        afficherDonnees();        
    }
}
